<template>
    <a-spin :spinning="confirmLoading">
        <a-table
            style="margin-top: 10px"
            ref="table"
            size="middle"
            :scroll="{x:true}"
            bordered
            :rowKey="safeRowKey"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            class="j-table-force-nowrap1">
          </a-table>
    </a-spin>
  </template>
  
  <script>
  
    import { httpAction, getAction, putAction } from '@/api/manage'
    import { validateDuplicateValue } from '@/utils/util'
    import { JVXETypes } from '@/components/jeecg/JVxeTable'
  
    export default {
      name: 'PlanDetailForm',
      components: {
      },
      props: {
      },
      data () {
        return {
          model:{},
          labelCol: {
            xs: { span: 24 },
            sm: { span: 5 },
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 },
          },
          confirmLoading: false,
          validatorRules: {
          },
          url: {
            detailList: '/productionconfirmationbefore/erpProductionProcessConfirmationBefore/queryDetailListByPlanId',
          },
          loading: false,
          dataSource: [],
          ipagination: {
            current: 1,
            pageSize: 30,
            pageSizeOptions: ['10', '20', '30'],
            showTotal: (total, range) => {
            return range[0] + '-' + range[1] + ' 共' + total + '条'
            },
            showQuickJumper: true,
            showSizeChanger: true,
            total: 0
          },
          columns: [
          {
            title: '订单编号',
            align: 'center',
            dataIndex: 'orderCode',
          },
          {
            title: '物料编码',
            align: 'center',
            dataIndex: 'productCode',
          },
          {
            title: '物料名称',
            align: 'center',
            dataIndex: 'productBatchNo',
          },
          {
            title: '规格型号',
            align: 'center',
            dataIndex: 'materialSpec',
          },
          {
            title: '配方号',
            align: 'center',
            dataIndex: 'formulaNumber',
          },
          {
            title: '生产锅',
            align: 'center',
            dataIndex: 'kettleName',
          },
          {
            title: '重量（吨）',
            align: 'center',
            dataIndex: 'planQuantity',
          },
          {
            title: '主单位',
            align: 'center',
            dataIndex: 'meUnit_dictText',
          },
          {
            title: '计划数量',
            align: 'center',
            dataIndex: 'planQuantity',
          },
          {
            title: '完成数量',
            align: 'center',
            dataIndex: 'completedQuantity',
          },
          {
            title: '剩余数量',
            align: 'center',
            dataIndex: 'residualQuantity',
          },
          {
            title: '包装车间',
            align: 'center',
            dataIndex: 'packshopCode_dictText',
          },
          ],
          dataSource: [],
        }
      },
      computed: {
        // 计算属性确保数据安全
        safeDataSource() {
        return this.dataSource.map(item => ({
            ...item,
            // 确保ID字段存在，若为null则生成临时ID
            id: item.scheduleId ?? this.generateTempId(item)
        }))
        }
      },
      created () {
         //备份model原始值
        this.modelDefault = JSON.parse(JSON.stringify(this.model));
      },
      methods: {
        detail (record) {
            this.loading = true;
            getAction(this.url.detailList, {id: record.planId}).then((res) => {
                if (res.success) {
                    this.dataSource = res.result.records || res.result || [];
                    if (res.result.total) {
                        this.ipagination.total = res.result.total
                    } else {
                        this.ipagination.total = 0
                    }
                } else {
                  this.$message.warning(res.message)
                }
            })
            .finally(() => {
                this.loading = false;
            })
        },
        // 安全获取rowKey
        safeRowKey(record) {
        return record.id || this.generateTempId(record)
        },
        // 生成临时ID
        generateTempId(record) {
        // 使用时间戳+随机数（确保唯一性）
        return `temp_${Date.now()}_${Math.floor(Math.random()*10000)}`
        },
      }
    }
  </script>
  <style scoped>
  </style>